package com.liujit.bms.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.admin.domain.model.BlogArticle;
import com.liujit.bms.admin.domain.model.BlogCategory;
import com.liujit.bms.admin.domain.params.BlogCategoryParams;
import com.liujit.bms.admin.domain.query.BlogCategoryQuery;
import com.liujit.bms.admin.domain.result.BlogCategoryResult;
import com.liujit.bms.admin.mapper.BlogCategoryMapper;
import com.liujit.bms.admin.service.BlogArticleService;
import com.liujit.bms.admin.service.BlogCategoryService;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.BusinessException;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 博客分类Service实现类
 * @Author liujun
 * @Date 2021-07-02 14:25:29
 */
@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory>
        implements BlogCategoryService {

    private BlogArticleService blogArticleService;

    @Autowired
    public void setBlogArticleService(BlogArticleService blogArticleService) {
        this.blogArticleService = blogArticleService;
    }

    @Override
    public PageBean<BlogCategoryResult> getPage(BlogCategoryQuery query) {
        // 排序
        if (StrUtil.isNotBlank(query.getSortName()) && StrUtil.isNotBlank(query.getSortOrder())) {
            query.setSortName(ColumnConstant.SEQ);
            query.setSortOrder(SqlKeyword.ASC.getSqlSegment());
        }
        Page<BlogCategory> page = Pagination.page(query);
        LambdaQueryWrapper<BlogCategory> wrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(query.getName())) {
            wrapper.like(BlogCategory::getName, query.getName());
        }
        if (query.getEnabled() != null) {
            wrapper.eq(BlogCategory::getEnabled, query.getEnabled());
        }
        if (query.getStartTime() != null) {
            wrapper.ge(BlogCategory::getCreateTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(BlogCategory::getCreateTime, query.getEndTime());
        }
        IPage<BlogCategory> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogCategoryResult.class);
    }

    @Override
    public Boolean save(BlogCategoryParams params) {
        BlogCategory entity = DozerConvertor.convertor(params, BlogCategory.class);
        // 添加
        if (params.getId() == null) {
            entity.setEnabled(GlobalConstant.ENABLED_YES);
            return super.save(entity);
        } else {
            BlogCategory blogCategory = super.getById(params.getId());
            if (blogCategory == null) {
                throw new DataNotFoundException();
            }
            return super.updateById(entity);
        }
    }

    @Override
    public BlogCategoryResult get(Long id) {
        BlogCategory blogCategory = super.getById(id);
        if (blogCategory == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(blogCategory, BlogCategoryResult.class);
    }

    @Override
    public Boolean del(BaseParams params) {
        BlogCategory blogCategory = super.getById(params.getId());
        if (blogCategory == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<BlogArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogArticle::getCategoryId, params.getId());
        Integer count = blogArticleService.count(wrapper);
        if (count > 0) {
            throw new BusinessException("分类下有文章，不能删除");
        }
        return super.removeById(params.getId());
    }

    @Override
    public Boolean enabled(BaseParams params) {
        BlogCategory blogCategory = super.getById(params.getId());
        if (blogCategory == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new BlogCategory() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_YES);
        }});
    }

    @Override
    public Boolean disabled(BaseParams params) {
        BlogCategory blogCategory = super.getById(params.getId());
        if (blogCategory == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<BlogArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogArticle::getCategoryId, params.getId());
        Integer count = blogArticleService.count(wrapper);
        if (count > 0) {
            throw new BusinessException("分类下有文章，不能禁用");
        }
        return super.updateById(new BlogCategory() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_NO);
        }});
    }

    @Override
    public Integer getCountByName(Long id, String name) {
        LambdaQueryWrapper<BlogCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogCategory::getName, name);
        if (id != null) {
            wrapper.ne(BlogCategory::getId, id);
        }
        return super.count(wrapper);
    }

    @Override
    public List<BlogCategoryResult> getList() {
        LambdaQueryWrapper<BlogCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogCategory::getEnabled, GlobalConstant.ENABLED_YES);
        List<BlogCategory> list = super.list(wrapper);
        return DozerConvertor.convertor(list, BlogCategoryResult.class);
    }

    @Override
    public String getNameById(Long categoryId) {
        LambdaQueryWrapper<BlogCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BlogCategory::getName);
        wrapper.eq(BlogCategory::getId, categoryId);
        return super.getObj(wrapper, item -> Convert.toStr(item));
    }
}
